package cn.wokoba.config;

import cn.wokoba.service.dcc.DccService;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
@EnableConfigurationProperties(NacosConfigProperties.class)
public class NacosConfig {


    //TODO 后续进行拆分
    @Bean
    NacosService nacosConfigService(NacosConfigProperties configProperties, DccService dccService) throws NacosException {
        Properties properties = new Properties();
        properties.put("serverAddr", configProperties.getServerAddr());

        NacosService nacosService = new NacosService(NacosFactory.createConfigService(properties),
                configProperties.getDataId(), configProperties.getGroup());
        dccService.setContent(nacosService.getConfig(5000));
        //添加动态变更监听
        nacosService.addListener(dccService);
        return nacosService;
    }

}
